<?php

declare(strict_types=1);

namespace app\store\controller\tools;

use app\store\controller\Controller;
use app\store\service\tools\Goods as goodService;
use think\App;
use think\response\Json;

class Goods extends Controller
{

    private $goodsService;
    public function __construct(App $app,goodService $goodsService)
    {
        parent::__construct($app);
        $this->goodsService = $goodsService;

    }

    //分类
    public function category():Json
    {
        $type = $this->request->param('type',1);
        $two_id = $this->request->param('two_id','');
        if(!in_array($type,[1,2])){
            return $this->renderError('参数错误');
        }
        if($type == 2){
            if(empty($two_id)) return $this->renderError('参数错误');
        }
        $list = $this->goodsService->getCateGoryList($type,$two_id);
        return $this->renderSuccess(compact('list'));
    }


    public function list():Json
    {
        $params = $this->request->param();
        $list = $this->goodsService->getList($params);
        return  $this->renderSuccess(compact('list'));
    }


    //新增编号
    public function add():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'series_id' => 'require',
                'part_id' => 'require',
                'style_id' => 'require',
                'batch_id' => 'require',
                'code' => 'require',
                'user_id' => 'integer'
            ]
        );
        $res = $this->goodsService->add($params);
        return $this->renderSuccess($res);
    }

    //编辑编号
    public function edit():Json
    {
        $params = $this->request->post();
        if(empty($params['id']) || empty($params['series']) || empty($params['part_name']) || empty($params['style'] || empty($params['batch'] || empty($params['code'])))){
            return  $this->renderError('参数错误');
        }
        $res = $this->goodsService->update($params);
        if(!$res){
            return $this->renderError('更新编号失败');
        }
        return $this->renderSuccess('更新编号成功');
    }


    //删除编号
    public function delete():Json
    {
        $params = $this->request->get();
        if(empty($params['id'])){
            return $this->renderError('参数错误');
        }
        $res = $this->goodsService->delete($params['id']);
        if(!$res){
            return $this->renderError('删除编号失败');
        }
        return $this->renderSuccess('删除编号成功');
    }

    //详情
    public function detail():Json
    {
        $params = $this->request->get();
        if(empty($params['id'])){
            return $this->renderError('参数错误');
        }
        $res = $this->goodsService->detail($params['id']);
        if(!$res){
            return $this->renderError('获取详情失败');
        }
        return $this->renderSuccess($res);
    }

    //默认列表
    public function defaultCate(): Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'one_id' => 'integer',
                'two_id' => 'integer',
            ]
        );
        $res = $this->goodsService->getDefaultCate($params);
        return $this->renderSuccess($res);
    }

    //保存批次
    public function saveBatch():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'name' => 'require'
            ]
        );
        if(empty($params['id'])){
            $this->validate(
                $params,
                ['two_id' => 'require|integer']
            );
        }else{
            $this->validate(
                $params,
                ['id' => 'require|integer']
            );
        }
        $res = $this->goodsService->saveBatch($params);
        if(empty($params['id'])){
            return $this->renderSuccess($res);
        }else{
            if(!$res){
                return $this->renderError('保存异常');
            }
            return $this->renderSuccess([],'保存成功');
        }
    }

    /**更改热门属性
     * @return Json
     */
    public function changeHotAttribute():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'style_id' => 'require|integer',
                'is_hot' => 'require|integer'  // 1 热门 0冷门
            ]
        );
        $res = $this->goodsService->getChangeHotAttribute($params);
        if(!$res){
            return $this->renderError('保存异常');
        }
        return $this->renderSuccess([],'操作成功');
    }


}